package com.hezhi.trafficcopsys.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.hezhi.framework.dao.DAOUtil;
import com.hezhi.framework.exception.APPException;
import com.hezhi.framework.service.impl.BaseServiceImpl;
import com.hezhi.trafficcopsys.domain.SpecialCar;
import com.hezhi.trafficcopsys.persistence.dao.SpecialCarDao;
import com.hezhi.trafficcopsys.persistence.dao.impl.SpecialCarDaoImpl;
import com.hezhi.trafficcopsys.service.SpecialCarService;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class SpecialCarServiceImpl extends BaseServiceImpl implements SpecialCarService{
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	private SpecialCarDao SpecialCarDao = null;
	
	public SpecialCarServiceImpl(){
		SpecialCarDao = new SpecialCarDaoImpl();
	}

	@Override
	public SpecialCar findSpecialCar(String SpecialCarname) {
		return SpecialCarDao.getSpecialCar(SpecialCarname);
	}
	
	@Override
	public List<SpecialCar> listSpecialCarByPage(int pageSize, int pageNo){
		return SpecialCarDao.listSpecialCarByPage( pageSize, pageNo);
	}

	@Override
	public boolean isExistedSpecialCar(String SpecialCarname) throws APPException{
		boolean isExisted = false;
		if( SpecialCarDao.getSpecialCar(SpecialCarname) != null ){
			isExisted = true;
		} else {
			throw new APPException("用户：" + SpecialCarname + "不存在！");
		}
		return isExisted;
	}

	@Override
	public int addSpecialCar(SpecialCar SpecialCar) {
		return SpecialCarDao.addSpecialCar(SpecialCar);
	}

	@Override
	public int delSpecialCars(String[] SpecialCarname ) {
		return SpecialCarDao.delSpecialCar(SpecialCarname);
	}
	@Override
	public int editSpecialCar(SpecialCar SpecialCar) {
		return SpecialCarDao.editSpecialCar(SpecialCar);
	}
	
	@Override
	public int getTotalSize() {
		return SpecialCarDao.getTotalSize();
	}
	
	public int loadSpecialCarsFromFile(File file) throws APPException {

		List<SpecialCar> listRecords = new ArrayList<SpecialCar>();
		FileInputStream fis = null;
		BufferedReader reader= null;
		Connection conn = null;
		try {
			
			//基于myFile创建一个文件输入流  
	        fis = new FileInputStream(file);  
	          
			reader=new BufferedReader(new InputStreamReader(fis)); 
			List<String> listCarNums = new ArrayList<String>(100);
			SpecialCar specialCar = null;
			String strLine = null;
			conn = DAOUtil.getSQLiteDBConnection();
			String sql = "insert into specialCar (carNo) values(?)";
			while((strLine = reader.readLine()) != null){
				if( !listCarNums.contains(strLine.trim())){
					listCarNums.add(strLine.trim());
					specialCar = new SpecialCar();
					specialCar.setCarNo(strLine.trim());
					listRecords.add(specialCar);
					
					SpecialCarDao.execute(conn, sql, strLine.trim());
				}
			}
			
		} catch (FileNotFoundException e) {
			log.error("没有找到文件！");
			throw new APPException("没有找到文件！");
		} catch (IOException e) {
			log.error("读取文件时出现错误！");
			throw new APPException("读取文件时出现错误！");
		} finally {
			try{
				if( fis != null){
					fis.close();
				}
				if( reader != null){
					reader.close();
				}
			} catch (Exception e){
				log.error("关闭文件时出现错误！");
			}
			DAOUtil.closeJDBCConnection(conn);
		}
		return listRecords.size();
	}
}
